{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "coral-vault",
   "metadata": {},
   "source": [
    "# 习题 5.5\n",
    "\n",
    "![习题 5.5 描述](https://tva1.sinaimg.cn/large/008i3skNly1grc30ihsyjj31l60dcq89.jpg)\n",
    "![习题 5.5 题目](https://tva1.sinaimg.cn/large/008i3skNly1grc32f19fqj31760fgtej.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sealed-subscriber",
   "metadata": {},
   "source": [
    "鸢尾花（iris）分类是常用数据集，在 R 有内置："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "standard-president",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 150 × 5</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>Sepal.Length</th><th scope=col>Sepal.Width</th><th scope=col>Petal.Length</th><th scope=col>Petal.Width</th><th scope=col>Species</th></tr>\n",
       "\t<tr><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;fct&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>5.1</td><td>3.5</td><td>1.4</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>4.9</td><td>3.0</td><td>1.4</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>4.7</td><td>3.2</td><td>1.3</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>4.6</td><td>3.1</td><td>1.5</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.0</td><td>3.6</td><td>1.4</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.4</td><td>3.9</td><td>1.7</td><td>0.4</td><td>setosa</td></tr>\n",
       "\t<tr><td>4.6</td><td>3.4</td><td>1.4</td><td>0.3</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.0</td><td>3.4</td><td>1.5</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>4.4</td><td>2.9</td><td>1.4</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>4.9</td><td>3.1</td><td>1.5</td><td>0.1</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.4</td><td>3.7</td><td>1.5</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>4.8</td><td>3.4</td><td>1.6</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>4.8</td><td>3.0</td><td>1.4</td><td>0.1</td><td>setosa</td></tr>\n",
       "\t<tr><td>4.3</td><td>3.0</td><td>1.1</td><td>0.1</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.8</td><td>4.0</td><td>1.2</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.7</td><td>4.4</td><td>1.5</td><td>0.4</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.4</td><td>3.9</td><td>1.3</td><td>0.4</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.1</td><td>3.5</td><td>1.4</td><td>0.3</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.7</td><td>3.8</td><td>1.7</td><td>0.3</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.1</td><td>3.8</td><td>1.5</td><td>0.3</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.4</td><td>3.4</td><td>1.7</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.1</td><td>3.7</td><td>1.5</td><td>0.4</td><td>setosa</td></tr>\n",
       "\t<tr><td>4.6</td><td>3.6</td><td>1.0</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.1</td><td>3.3</td><td>1.7</td><td>0.5</td><td>setosa</td></tr>\n",
       "\t<tr><td>4.8</td><td>3.4</td><td>1.9</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.0</td><td>3.0</td><td>1.6</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.0</td><td>3.4</td><td>1.6</td><td>0.4</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.2</td><td>3.5</td><td>1.5</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>5.2</td><td>3.4</td><td>1.4</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>4.7</td><td>3.2</td><td>1.6</td><td>0.2</td><td>setosa</td></tr>\n",
       "\t<tr><td>⋮</td><td>⋮</td><td>⋮</td><td>⋮</td><td>⋮</td></tr>\n",
       "\t<tr><td>6.9</td><td>3.2</td><td>5.7</td><td>2.3</td><td>virginica</td></tr>\n",
       "\t<tr><td>5.6</td><td>2.8</td><td>4.9</td><td>2.0</td><td>virginica</td></tr>\n",
       "\t<tr><td>7.7</td><td>2.8</td><td>6.7</td><td>2.0</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.3</td><td>2.7</td><td>4.9</td><td>1.8</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.7</td><td>3.3</td><td>5.7</td><td>2.1</td><td>virginica</td></tr>\n",
       "\t<tr><td>7.2</td><td>3.2</td><td>6.0</td><td>1.8</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.2</td><td>2.8</td><td>4.8</td><td>1.8</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.1</td><td>3.0</td><td>4.9</td><td>1.8</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.4</td><td>2.8</td><td>5.6</td><td>2.1</td><td>virginica</td></tr>\n",
       "\t<tr><td>7.2</td><td>3.0</td><td>5.8</td><td>1.6</td><td>virginica</td></tr>\n",
       "\t<tr><td>7.4</td><td>2.8</td><td>6.1</td><td>1.9</td><td>virginica</td></tr>\n",
       "\t<tr><td>7.9</td><td>3.8</td><td>6.4</td><td>2.0</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.4</td><td>2.8</td><td>5.6</td><td>2.2</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.3</td><td>2.8</td><td>5.1</td><td>1.5</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.1</td><td>2.6</td><td>5.6</td><td>1.4</td><td>virginica</td></tr>\n",
       "\t<tr><td>7.7</td><td>3.0</td><td>6.1</td><td>2.3</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.3</td><td>3.4</td><td>5.6</td><td>2.4</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.4</td><td>3.1</td><td>5.5</td><td>1.8</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.0</td><td>3.0</td><td>4.8</td><td>1.8</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.9</td><td>3.1</td><td>5.4</td><td>2.1</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.7</td><td>3.1</td><td>5.6</td><td>2.4</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.9</td><td>3.1</td><td>5.1</td><td>2.3</td><td>virginica</td></tr>\n",
       "\t<tr><td>5.8</td><td>2.7</td><td>5.1</td><td>1.9</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.8</td><td>3.2</td><td>5.9</td><td>2.3</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.7</td><td>3.3</td><td>5.7</td><td>2.5</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.7</td><td>3.0</td><td>5.2</td><td>2.3</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.3</td><td>2.5</td><td>5.0</td><td>1.9</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.5</td><td>3.0</td><td>5.2</td><td>2.0</td><td>virginica</td></tr>\n",
       "\t<tr><td>6.2</td><td>3.4</td><td>5.4</td><td>2.3</td><td>virginica</td></tr>\n",
       "\t<tr><td>5.9</td><td>3.0</td><td>5.1</td><td>1.8</td><td>virginica</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 150 × 5\n",
       "\\begin{tabular}{lllll}\n",
       " Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\\\\n",
       " <dbl> & <dbl> & <dbl> & <dbl> & <fct>\\\\\n",
       "\\hline\n",
       "\t 5.1 & 3.5 & 1.4 & 0.2 & setosa\\\\\n",
       "\t 4.9 & 3.0 & 1.4 & 0.2 & setosa\\\\\n",
       "\t 4.7 & 3.2 & 1.3 & 0.2 & setosa\\\\\n",
       "\t 4.6 & 3.1 & 1.5 & 0.2 & setosa\\\\\n",
       "\t 5.0 & 3.6 & 1.4 & 0.2 & setosa\\\\\n",
       "\t 5.4 & 3.9 & 1.7 & 0.4 & setosa\\\\\n",
       "\t 4.6 & 3.4 & 1.4 & 0.3 & setosa\\\\\n",
       "\t 5.0 & 3.4 & 1.5 & 0.2 & setosa\\\\\n",
       "\t 4.4 & 2.9 & 1.4 & 0.2 & setosa\\\\\n",
       "\t 4.9 & 3.1 & 1.5 & 0.1 & setosa\\\\\n",
       "\t 5.4 & 3.7 & 1.5 & 0.2 & setosa\\\\\n",
       "\t 4.8 & 3.4 & 1.6 & 0.2 & setosa\\\\\n",
       "\t 4.8 & 3.0 & 1.4 & 0.1 & setosa\\\\\n",
       "\t 4.3 & 3.0 & 1.1 & 0.1 & setosa\\\\\n",
       "\t 5.8 & 4.0 & 1.2 & 0.2 & setosa\\\\\n",
       "\t 5.7 & 4.4 & 1.5 & 0.4 & setosa\\\\\n",
       "\t 5.4 & 3.9 & 1.3 & 0.4 & setosa\\\\\n",
       "\t 5.1 & 3.5 & 1.4 & 0.3 & setosa\\\\\n",
       "\t 5.7 & 3.8 & 1.7 & 0.3 & setosa\\\\\n",
       "\t 5.1 & 3.8 & 1.5 & 0.3 & setosa\\\\\n",
       "\t 5.4 & 3.4 & 1.7 & 0.2 & setosa\\\\\n",
       "\t 5.1 & 3.7 & 1.5 & 0.4 & setosa\\\\\n",
       "\t 4.6 & 3.6 & 1.0 & 0.2 & setosa\\\\\n",
       "\t 5.1 & 3.3 & 1.7 & 0.5 & setosa\\\\\n",
       "\t 4.8 & 3.4 & 1.9 & 0.2 & setosa\\\\\n",
       "\t 5.0 & 3.0 & 1.6 & 0.2 & setosa\\\\\n",
       "\t 5.0 & 3.4 & 1.6 & 0.4 & setosa\\\\\n",
       "\t 5.2 & 3.5 & 1.5 & 0.2 & setosa\\\\\n",
       "\t 5.2 & 3.4 & 1.4 & 0.2 & setosa\\\\\n",
       "\t 4.7 & 3.2 & 1.6 & 0.2 & setosa\\\\\n",
       "\t ⋮ & ⋮ & ⋮ & ⋮ & ⋮\\\\\n",
       "\t 6.9 & 3.2 & 5.7 & 2.3 & virginica\\\\\n",
       "\t 5.6 & 2.8 & 4.9 & 2.0 & virginica\\\\\n",
       "\t 7.7 & 2.8 & 6.7 & 2.0 & virginica\\\\\n",
       "\t 6.3 & 2.7 & 4.9 & 1.8 & virginica\\\\\n",
       "\t 6.7 & 3.3 & 5.7 & 2.1 & virginica\\\\\n",
       "\t 7.2 & 3.2 & 6.0 & 1.8 & virginica\\\\\n",
       "\t 6.2 & 2.8 & 4.8 & 1.8 & virginica\\\\\n",
       "\t 6.1 & 3.0 & 4.9 & 1.8 & virginica\\\\\n",
       "\t 6.4 & 2.8 & 5.6 & 2.1 & virginica\\\\\n",
       "\t 7.2 & 3.0 & 5.8 & 1.6 & virginica\\\\\n",
       "\t 7.4 & 2.8 & 6.1 & 1.9 & virginica\\\\\n",
       "\t 7.9 & 3.8 & 6.4 & 2.0 & virginica\\\\\n",
       "\t 6.4 & 2.8 & 5.6 & 2.2 & virginica\\\\\n",
       "\t 6.3 & 2.8 & 5.1 & 1.5 & virginica\\\\\n",
       "\t 6.1 & 2.6 & 5.6 & 1.4 & virginica\\\\\n",
       "\t 7.7 & 3.0 & 6.1 & 2.3 & virginica\\\\\n",
       "\t 6.3 & 3.4 & 5.6 & 2.4 & virginica\\\\\n",
       "\t 6.4 & 3.1 & 5.5 & 1.8 & virginica\\\\\n",
       "\t 6.0 & 3.0 & 4.8 & 1.8 & virginica\\\\\n",
       "\t 6.9 & 3.1 & 5.4 & 2.1 & virginica\\\\\n",
       "\t 6.7 & 3.1 & 5.6 & 2.4 & virginica\\\\\n",
       "\t 6.9 & 3.1 & 5.1 & 2.3 & virginica\\\\\n",
       "\t 5.8 & 2.7 & 5.1 & 1.9 & virginica\\\\\n",
       "\t 6.8 & 3.2 & 5.9 & 2.3 & virginica\\\\\n",
       "\t 6.7 & 3.3 & 5.7 & 2.5 & virginica\\\\\n",
       "\t 6.7 & 3.0 & 5.2 & 2.3 & virginica\\\\\n",
       "\t 6.3 & 2.5 & 5.0 & 1.9 & virginica\\\\\n",
       "\t 6.5 & 3.0 & 5.2 & 2.0 & virginica\\\\\n",
       "\t 6.2 & 3.4 & 5.4 & 2.3 & virginica\\\\\n",
       "\t 5.9 & 3.0 & 5.1 & 1.8 & virginica\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 150 × 5\n",
       "\n",
       "| Sepal.Length &lt;dbl&gt; | Sepal.Width &lt;dbl&gt; | Petal.Length &lt;dbl&gt; | Petal.Width &lt;dbl&gt; | Species &lt;fct&gt; |\n",
       "|---|---|---|---|---|\n",
       "| 5.1 | 3.5 | 1.4 | 0.2 | setosa |\n",
       "| 4.9 | 3.0 | 1.4 | 0.2 | setosa |\n",
       "| 4.7 | 3.2 | 1.3 | 0.2 | setosa |\n",
       "| 4.6 | 3.1 | 1.5 | 0.2 | setosa |\n",
       "| 5.0 | 3.6 | 1.4 | 0.2 | setosa |\n",
       "| 5.4 | 3.9 | 1.7 | 0.4 | setosa |\n",
       "| 4.6 | 3.4 | 1.4 | 0.3 | setosa |\n",
       "| 5.0 | 3.4 | 1.5 | 0.2 | setosa |\n",
       "| 4.4 | 2.9 | 1.4 | 0.2 | setosa |\n",
       "| 4.9 | 3.1 | 1.5 | 0.1 | setosa |\n",
       "| 5.4 | 3.7 | 1.5 | 0.2 | setosa |\n",
       "| 4.8 | 3.4 | 1.6 | 0.2 | setosa |\n",
       "| 4.8 | 3.0 | 1.4 | 0.1 | setosa |\n",
       "| 4.3 | 3.0 | 1.1 | 0.1 | setosa |\n",
       "| 5.8 | 4.0 | 1.2 | 0.2 | setosa |\n",
       "| 5.7 | 4.4 | 1.5 | 0.4 | setosa |\n",
       "| 5.4 | 3.9 | 1.3 | 0.4 | setosa |\n",
       "| 5.1 | 3.5 | 1.4 | 0.3 | setosa |\n",
       "| 5.7 | 3.8 | 1.7 | 0.3 | setosa |\n",
       "| 5.1 | 3.8 | 1.5 | 0.3 | setosa |\n",
       "| 5.4 | 3.4 | 1.7 | 0.2 | setosa |\n",
       "| 5.1 | 3.7 | 1.5 | 0.4 | setosa |\n",
       "| 4.6 | 3.6 | 1.0 | 0.2 | setosa |\n",
       "| 5.1 | 3.3 | 1.7 | 0.5 | setosa |\n",
       "| 4.8 | 3.4 | 1.9 | 0.2 | setosa |\n",
       "| 5.0 | 3.0 | 1.6 | 0.2 | setosa |\n",
       "| 5.0 | 3.4 | 1.6 | 0.4 | setosa |\n",
       "| 5.2 | 3.5 | 1.5 | 0.2 | setosa |\n",
       "| 5.2 | 3.4 | 1.4 | 0.2 | setosa |\n",
       "| 4.7 | 3.2 | 1.6 | 0.2 | setosa |\n",
       "| ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |\n",
       "| 6.9 | 3.2 | 5.7 | 2.3 | virginica |\n",
       "| 5.6 | 2.8 | 4.9 | 2.0 | virginica |\n",
       "| 7.7 | 2.8 | 6.7 | 2.0 | virginica |\n",
       "| 6.3 | 2.7 | 4.9 | 1.8 | virginica |\n",
       "| 6.7 | 3.3 | 5.7 | 2.1 | virginica |\n",
       "| 7.2 | 3.2 | 6.0 | 1.8 | virginica |\n",
       "| 6.2 | 2.8 | 4.8 | 1.8 | virginica |\n",
       "| 6.1 | 3.0 | 4.9 | 1.8 | virginica |\n",
       "| 6.4 | 2.8 | 5.6 | 2.1 | virginica |\n",
       "| 7.2 | 3.0 | 5.8 | 1.6 | virginica |\n",
       "| 7.4 | 2.8 | 6.1 | 1.9 | virginica |\n",
       "| 7.9 | 3.8 | 6.4 | 2.0 | virginica |\n",
       "| 6.4 | 2.8 | 5.6 | 2.2 | virginica |\n",
       "| 6.3 | 2.8 | 5.1 | 1.5 | virginica |\n",
       "| 6.1 | 2.6 | 5.6 | 1.4 | virginica |\n",
       "| 7.7 | 3.0 | 6.1 | 2.3 | virginica |\n",
       "| 6.3 | 3.4 | 5.6 | 2.4 | virginica |\n",
       "| 6.4 | 3.1 | 5.5 | 1.8 | virginica |\n",
       "| 6.0 | 3.0 | 4.8 | 1.8 | virginica |\n",
       "| 6.9 | 3.1 | 5.4 | 2.1 | virginica |\n",
       "| 6.7 | 3.1 | 5.6 | 2.4 | virginica |\n",
       "| 6.9 | 3.1 | 5.1 | 2.3 | virginica |\n",
       "| 5.8 | 2.7 | 5.1 | 1.9 | virginica |\n",
       "| 6.8 | 3.2 | 5.9 | 2.3 | virginica |\n",
       "| 6.7 | 3.3 | 5.7 | 2.5 | virginica |\n",
       "| 6.7 | 3.0 | 5.2 | 2.3 | virginica |\n",
       "| 6.3 | 2.5 | 5.0 | 1.9 | virginica |\n",
       "| 6.5 | 3.0 | 5.2 | 2.0 | virginica |\n",
       "| 6.2 | 3.4 | 5.4 | 2.3 | virginica |\n",
       "| 5.9 | 3.0 | 5.1 | 1.8 | virginica |\n",
       "\n"
      ],
      "text/plain": [
       "    Sepal.Length Sepal.Width Petal.Length Petal.Width Species  \n",
       "1   5.1          3.5         1.4          0.2         setosa   \n",
       "2   4.9          3.0         1.4          0.2         setosa   \n",
       "3   4.7          3.2         1.3          0.2         setosa   \n",
       "4   4.6          3.1         1.5          0.2         setosa   \n",
       "5   5.0          3.6         1.4          0.2         setosa   \n",
       "6   5.4          3.9         1.7          0.4         setosa   \n",
       "7   4.6          3.4         1.4          0.3         setosa   \n",
       "8   5.0          3.4         1.5          0.2         setosa   \n",
       "9   4.4          2.9         1.4          0.2         setosa   \n",
       "10  4.9          3.1         1.5          0.1         setosa   \n",
       "11  5.4          3.7         1.5          0.2         setosa   \n",
       "12  4.8          3.4         1.6          0.2         setosa   \n",
       "13  4.8          3.0         1.4          0.1         setosa   \n",
       "14  4.3          3.0         1.1          0.1         setosa   \n",
       "15  5.8          4.0         1.2          0.2         setosa   \n",
       "16  5.7          4.4         1.5          0.4         setosa   \n",
       "17  5.4          3.9         1.3          0.4         setosa   \n",
       "18  5.1          3.5         1.4          0.3         setosa   \n",
       "19  5.7          3.8         1.7          0.3         setosa   \n",
       "20  5.1          3.8         1.5          0.3         setosa   \n",
       "21  5.4          3.4         1.7          0.2         setosa   \n",
       "22  5.1          3.7         1.5          0.4         setosa   \n",
       "23  4.6          3.6         1.0          0.2         setosa   \n",
       "24  5.1          3.3         1.7          0.5         setosa   \n",
       "25  4.8          3.4         1.9          0.2         setosa   \n",
       "26  5.0          3.0         1.6          0.2         setosa   \n",
       "27  5.0          3.4         1.6          0.4         setosa   \n",
       "28  5.2          3.5         1.5          0.2         setosa   \n",
       "29  5.2          3.4         1.4          0.2         setosa   \n",
       "30  4.7          3.2         1.6          0.2         setosa   \n",
       "⋮   ⋮            ⋮           ⋮            ⋮           ⋮        \n",
       "121 6.9          3.2         5.7          2.3         virginica\n",
       "122 5.6          2.8         4.9          2.0         virginica\n",
       "123 7.7          2.8         6.7          2.0         virginica\n",
       "124 6.3          2.7         4.9          1.8         virginica\n",
       "125 6.7          3.3         5.7          2.1         virginica\n",
       "126 7.2          3.2         6.0          1.8         virginica\n",
       "127 6.2          2.8         4.8          1.8         virginica\n",
       "128 6.1          3.0         4.9          1.8         virginica\n",
       "129 6.4          2.8         5.6          2.1         virginica\n",
       "130 7.2          3.0         5.8          1.6         virginica\n",
       "131 7.4          2.8         6.1          1.9         virginica\n",
       "132 7.9          3.8         6.4          2.0         virginica\n",
       "133 6.4          2.8         5.6          2.2         virginica\n",
       "134 6.3          2.8         5.1          1.5         virginica\n",
       "135 6.1          2.6         5.6          1.4         virginica\n",
       "136 7.7          3.0         6.1          2.3         virginica\n",
       "137 6.3          3.4         5.6          2.4         virginica\n",
       "138 6.4          3.1         5.5          1.8         virginica\n",
       "139 6.0          3.0         4.8          1.8         virginica\n",
       "140 6.9          3.1         5.4          2.1         virginica\n",
       "141 6.7          3.1         5.6          2.4         virginica\n",
       "142 6.9          3.1         5.1          2.3         virginica\n",
       "143 5.8          2.7         5.1          1.9         virginica\n",
       "144 6.8          3.2         5.9          2.3         virginica\n",
       "145 6.7          3.3         5.7          2.5         virginica\n",
       "146 6.7          3.0         5.2          2.3         virginica\n",
       "147 6.3          2.5         5.0          1.9         virginica\n",
       "148 6.5          3.0         5.2          2.0         virginica\n",
       "149 6.2          3.4         5.4          2.3         virginica\n",
       "150 5.9          3.0         5.1          1.8         virginica"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data(iris)\n",
    "attach(iris)\n",
    "iris"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "employed-cardiff",
   "metadata": {},
   "source": [
    "这些数据形状与书上题目所给数据一直，只是 $x_i$ 值相差常数倍（10x）。\n",
    "为保证这个数据集确是和书上一致，下面随意验证几个："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "burning-inspector",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 1 × 5</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>Sepal.Length</th><th scope=col>Sepal.Width</th><th scope=col>Petal.Length</th><th scope=col>Petal.Width</th><th scope=col>Species</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;fct&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>50</th><td>5</td><td>3.3</td><td>1.4</td><td>0.2</td><td>setosa</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 1 × 5\n",
       "\\begin{tabular}{r|lllll}\n",
       "  & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\\\\n",
       "  & <dbl> & <dbl> & <dbl> & <dbl> & <fct>\\\\\n",
       "\\hline\n",
       "\t50 & 5 & 3.3 & 1.4 & 0.2 & setosa\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 1 × 5\n",
       "\n",
       "| <!--/--> | Sepal.Length &lt;dbl&gt; | Sepal.Width &lt;dbl&gt; | Petal.Length &lt;dbl&gt; | Petal.Width &lt;dbl&gt; | Species &lt;fct&gt; |\n",
       "|---|---|---|---|---|---|\n",
       "| 50 | 5 | 3.3 | 1.4 | 0.2 | setosa |\n",
       "\n"
      ],
      "text/plain": [
       "   Sepal.Length Sepal.Width Petal.Length Petal.Width Species\n",
       "50 5            3.3         1.4          0.2         setosa "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "iris[Sepal.Length==5.0 & Sepal.Width==3.3, ]    # 书上的 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "caring-republic",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 1 × 5</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>Sepal.Length</th><th scope=col>Sepal.Width</th><th scope=col>Petal.Length</th><th scope=col>Petal.Width</th><th scope=col>Species</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;fct&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>55</th><td>6.5</td><td>2.8</td><td>4.6</td><td>1.5</td><td>versicolor</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 1 × 5\n",
       "\\begin{tabular}{r|lllll}\n",
       "  & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\\\\n",
       "  & <dbl> & <dbl> & <dbl> & <dbl> & <fct>\\\\\n",
       "\\hline\n",
       "\t55 & 6.5 & 2.8 & 4.6 & 1.5 & versicolor\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 1 × 5\n",
       "\n",
       "| <!--/--> | Sepal.Length &lt;dbl&gt; | Sepal.Width &lt;dbl&gt; | Petal.Length &lt;dbl&gt; | Petal.Width &lt;dbl&gt; | Species &lt;fct&gt; |\n",
       "|---|---|---|---|---|---|\n",
       "| 55 | 6.5 | 2.8 | 4.6 | 1.5 | versicolor |\n",
       "\n"
      ],
      "text/plain": [
       "   Sepal.Length Sepal.Width Petal.Length Petal.Width Species   \n",
       "55 6.5          2.8         4.6          1.5         versicolor"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "iris[Sepal.Length==6.5 & Sepal.Width==2.8, ]    # 书上的 51"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "egyptian-florence",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 2 × 5</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>Sepal.Length</th><th scope=col>Sepal.Width</th><th scope=col>Petal.Length</th><th scope=col>Petal.Width</th><th scope=col>Species</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;fct&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>129</th><td>6.4</td><td>2.8</td><td>5.6</td><td>2.1</td><td>virginica</td></tr>\n",
       "\t<tr><th scope=row>133</th><td>6.4</td><td>2.8</td><td>5.6</td><td>2.2</td><td>virginica</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 2 × 5\n",
       "\\begin{tabular}{r|lllll}\n",
       "  & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\\\\n",
       "  & <dbl> & <dbl> & <dbl> & <dbl> & <fct>\\\\\n",
       "\\hline\n",
       "\t129 & 6.4 & 2.8 & 5.6 & 2.1 & virginica\\\\\n",
       "\t133 & 6.4 & 2.8 & 5.6 & 2.2 & virginica\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 2 × 5\n",
       "\n",
       "| <!--/--> | Sepal.Length &lt;dbl&gt; | Sepal.Width &lt;dbl&gt; | Petal.Length &lt;dbl&gt; | Petal.Width &lt;dbl&gt; | Species &lt;fct&gt; |\n",
       "|---|---|---|---|---|---|\n",
       "| 129 | 6.4 | 2.8 | 5.6 | 2.1 | virginica |\n",
       "| 133 | 6.4 | 2.8 | 5.6 | 2.2 | virginica |\n",
       "\n"
      ],
      "text/plain": [
       "    Sepal.Length Sepal.Width Petal.Length Petal.Width Species  \n",
       "129 6.4          2.8         5.6          2.1         virginica\n",
       "133 6.4          2.8         5.6          2.2         virginica"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "iris[Sepal.Length==6.4 & Sepal.Width==2.8, ]    # 书上的 125、101"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "interesting-queensland",
   "metadata": {},
   "source": [
    "可以把数据放大 10 倍，得到书上的数据（这里不这么做）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "original-homework",
   "metadata": {},
   "outputs": [],
   "source": [
    "# iris[-5] <- iris[-5] * 10; iris"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "hired-assist",
   "metadata": {},
   "source": [
    "## (2)\n",
    "\n",
    "先做**总体协方差矩阵相同**的情况：\n",
    "\n",
    "只考虑 $X_2$、$X_4$ 即 `Sepal.Width` 和 `Petal.Width`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "copyrighted-circumstances",
   "metadata": {},
   "outputs": [],
   "source": [
    "library(MASS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "celtic-companion",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Call:\n",
       "lda(Species ~ Sepal.Width + Petal.Width, data = iris)\n",
       "\n",
       "Prior probabilities of groups:\n",
       "    setosa versicolor  virginica \n",
       " 0.3333333  0.3333333  0.3333333 \n",
       "\n",
       "Group means:\n",
       "           Sepal.Width Petal.Width\n",
       "setosa           3.428       0.246\n",
       "versicolor       2.770       1.326\n",
       "virginica        2.974       2.026\n",
       "\n",
       "Coefficients of linear discriminants:\n",
       "                  LD1       LD2\n",
       "Sepal.Width -1.986964 2.6800746\n",
       "Petal.Width  5.477136 0.8169648\n",
       "\n",
       "Proportion of trace:\n",
       "   LD1    LD2 \n",
       "0.9884 0.0116 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "l1 = lda(Species ~ Sepal.Width + Petal.Width, data=iris); l1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "organic-equality",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A matrix: 2 × 2 of type dbl</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>LD1</th><th scope=col>LD2</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>Sepal.Width</th><td>-1.986964</td><td>2.6800746</td></tr>\n",
       "\t<tr><th scope=row>Petal.Width</th><td> 5.477136</td><td>0.8169648</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A matrix: 2 × 2 of type dbl\n",
       "\\begin{tabular}{r|ll}\n",
       "  & LD1 & LD2\\\\\n",
       "\\hline\n",
       "\tSepal.Width & -1.986964 & 2.6800746\\\\\n",
       "\tPetal.Width &  5.477136 & 0.8169648\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A matrix: 2 × 2 of type dbl\n",
       "\n",
       "| <!--/--> | LD1 | LD2 |\n",
       "|---|---|---|\n",
       "| Sepal.Width | -1.986964 | 2.6800746 |\n",
       "| Petal.Width |  5.477136 | 0.8169648 |\n",
       "\n"
      ],
      "text/plain": [
       "            LD1       LD2      \n",
       "Sepal.Width -1.986964 2.6800746\n",
       "Petal.Width  5.477136 0.8169648"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "l1$scaling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "broke-escape",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "W_1 = -1.98696447*x_1 + 5.47713646*x_2 + \n",
      "W_2 = 2.6800746*x_1 + 0.81696482*x_2 + \n"
     ]
    }
   ],
   "source": [
    "# 输出线性判别函数\n",
    "cat(\"W_1 =\", paste(round(l1$scaling[1:2], 8), \"*x_\", 1:2, \" +\", sep=\"\"), '\\n')\n",
    "cat(\"W_2 =\", paste(round(l1$scaling[3:4], 8), \"*x_\", 1:2, \" +\", sep=\"\"), '\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "micro-distinction",
   "metadata": {},
   "source": [
    "得到线性判别函数：\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "W_1 &= -1.98696447 x_1 + 5.47713646 x_2 \\\\\n",
    "W_2 &= 2.6800746 x_1 + 0.81696482 x_2 \n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "precious-motion",
   "metadata": {},
   "source": [
    "为方便进行误判确认，封装一个工具函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "active-accounting",
   "metadata": {},
   "outputs": [],
   "source": [
    "prederr <- function(pred, true.group) {\n",
    "    # lda 判别分析预测误判\n",
    "    # Args:\n",
    "    #    pred: lda 的预测, e.g. predict(ldm(Species ~ ., data = data))\n",
    "    #    true.group: 实际的分类（data 的 label）\n",
    "    # Returns:\n",
    "    #    list(`tab`, `err`, `err.ratio`)\n",
    "    #      tab: 实际与预判交叉表\n",
    "    #      err: 错误分类数据列表\n",
    "    #      err.ratio: 误判率\n",
    "    \n",
    "    tab <- table(Species, pred$class)\n",
    "    err <- data.frame(pred, true.group)[pred$class != true.group,]\n",
    "    err.ratio <- length(err$class) / length(pred$class)\n",
    "    \n",
    "    # ret\n",
    "    list(`tab`=tab, `err`=err, `err.ratio`=err.ratio)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cardiac-boundary",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "$tab\n",
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         49         1\n",
       "  virginica       0          4        46\n",
       "\n",
       "$err\n",
       "         class posterior.setosa posterior.versicolor posterior.virginica\n",
       "71   virginica     9.920568e-18            0.2331276          0.76687239\n",
       "120 versicolor     1.919451e-19            0.7114697          0.28853027\n",
       "130 versicolor     3.125859e-15            0.8792078          0.12079219\n",
       "134 versicolor     5.868316e-15            0.9605066          0.03949344\n",
       "135 versicolor     1.037139e-14            0.9878440          0.01215599\n",
       "       x.LD1      x.LD2 true.group\n",
       "71  3.006460  0.8730808 versicolor\n",
       "120 3.350283 -2.0520832  virginica\n",
       "130 2.308425  0.1736730  virginica\n",
       "134 2.158105 -0.4440384  virginica\n",
       "135 2.007784 -1.0617498  virginica\n",
       "\n",
       "$err.ratio\n",
       "[1] 0.03333333\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pred1 <- predict(l1)\n",
    "prederr(pred1, Species)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "exposed-regulation",
   "metadata": {},
   "source": [
    "得到误判率 $3.33\\%$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "attempted-worth",
   "metadata": {},
   "source": [
    "交叉确认误判率："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "precise-enemy",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "$tab\n",
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         48         2\n",
       "  virginica       0          4        46\n",
       "\n",
       "$err\n",
       "         class       setosa versicolor   virginica true.group\n",
       "71   virginica 5.100762e-18  0.2004581 0.799541885 versicolor\n",
       "78   virginica 2.536707e-17  0.4845668 0.515433247 versicolor\n",
       "120 versicolor 2.400102e-19  0.7871770 0.212822971  virginica\n",
       "130 versicolor 1.021173e-15  0.8990896 0.100910442  virginica\n",
       "134 versicolor 1.776898e-15  0.9704931 0.029506851  virginica\n",
       "135 versicolor 3.229543e-15  0.9926251 0.007374932  virginica\n",
       "\n",
       "$err.ratio\n",
       "[1] 0.04\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "l1.cv <- lda(Species ~ Sepal.Width + Petal.Width, data=iris, CV=TRUE)\n",
    "prederr(data.frame(class=l1.cv$class, l1.cv$posterior), Species)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "numerous-burton",
   "metadata": {},
   "source": [
    "得到误判率 $4\\%$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "south-footwear",
   "metadata": {},
   "source": [
    "## (1) \n",
    "\n",
    "总体协方差矩阵不相同的情况，用 `qda` 来做："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "domestic-clearing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Call:\n",
       "qda(Species ~ Sepal.Width + Petal.Width, data = iris)\n",
       "\n",
       "Prior probabilities of groups:\n",
       "    setosa versicolor  virginica \n",
       " 0.3333333  0.3333333  0.3333333 \n",
       "\n",
       "Group means:\n",
       "           Sepal.Width Petal.Width\n",
       "setosa           3.428       0.246\n",
       "versicolor       2.770       1.326\n",
       "virginica        2.974       2.026"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "qd <- qda(Species ~ Sepal.Width + Petal.Width, data=iris); qd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sticky-least",
   "metadata": {},
   "source": [
    "预测，并查看误判情况："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "adjustable-monthly",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "$tab\n",
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         46         4\n",
       "  virginica       0          3        47\n",
       "\n",
       "$err\n",
       "         class posterior.setosa posterior.versicolor posterior.virginica\n",
       "69   virginica     3.612396e-38            0.1288885          0.87111146\n",
       "71   virginica     5.782918e-51            0.2345794          0.76542060\n",
       "78   virginica     7.578016e-46            0.3801197          0.61988031\n",
       "84   virginica     1.621740e-41            0.3894875          0.61051249\n",
       "130 versicolor     4.565572e-40            0.7817783          0.21822173\n",
       "134 versicolor     1.077297e-35            0.8674122          0.13258783\n",
       "135 versicolor     1.359084e-31            0.9204160          0.07958403\n",
       "    true.group\n",
       "69  versicolor\n",
       "71  versicolor\n",
       "78  versicolor\n",
       "84  versicolor\n",
       "130  virginica\n",
       "134  virginica\n",
       "135  virginica\n",
       "\n",
       "$err.ratio\n",
       "[1] 0.04666667\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "predq <- predict(qd)\n",
    "prederr(predq, Species)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "green-performance",
   "metadata": {},
   "source": [
    "得到误判率 $4.67\\%$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "incorporate-technique",
   "metadata": {},
   "source": [
    "交叉确认误判率："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "needed-honolulu",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "$tab\n",
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         45         5\n",
       "  virginica       0          3        47\n",
       "\n",
       "$err\n",
       "         class       setosa versicolor  virginica true.group\n",
       "69   virginica 4.037306e-38 0.02642376 0.97357624 versicolor\n",
       "71   virginica 6.294572e-51 0.16685743 0.83314257 versicolor\n",
       "73   virginica 7.169125e-37 0.48449460 0.51550540 versicolor\n",
       "78   virginica 8.162732e-46 0.33228997 0.66771003 versicolor\n",
       "84   virginica 1.761570e-41 0.33684744 0.66315256 versicolor\n",
       "130 versicolor 4.722285e-40 0.80861274 0.19138726  virginica\n",
       "134 versicolor 1.104200e-35 0.88907345 0.11092655  virginica\n",
       "135 versicolor 1.390787e-31 0.94188622 0.05811378  virginica\n",
       "\n",
       "$err.ratio\n",
       "[1] 0.05333333\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "qd.cv <- qda(Species ~ Sepal.Width + Petal.Width, data=iris, CV=TRUE)\n",
    "prederr(data.frame(class=qd.cv$class, qd.cv$posterior), Species)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "congressional-ticket",
   "metadata": {},
   "source": [
    "得到误判率 $5.33\\%$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "developing-folks",
   "metadata": {},
   "source": [
    "## （3）\n",
    "\n",
    "新样本："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "behind-backup",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 1 × 2</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>Sepal.Width</th><th scope=col>Petal.Width</th></tr>\n",
       "\t<tr><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>35</td><td>18</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 1 × 2\n",
       "\\begin{tabular}{ll}\n",
       " Sepal.Width & Petal.Width\\\\\n",
       " <dbl> & <dbl>\\\\\n",
       "\\hline\n",
       "\t 35 & 18\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 1 × 2\n",
       "\n",
       "| Sepal.Width &lt;dbl&gt; | Petal.Width &lt;dbl&gt; |\n",
       "|---|---|\n",
       "| 35 | 18 |\n",
       "\n"
      ],
      "text/plain": [
       "  Sepal.Width Petal.Width\n",
       "1 35          18         "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x0 = data.frame(Sepal.Width = c(35), Petal.Width=c(18)); x0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "perfect-situation",
   "metadata": {},
   "source": [
    "带入 lda 模型："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "reliable-citation",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<dl>\n",
       "\t<dt>$class</dt>\n",
       "\t\t<dd>virginica\n",
       "<details>\n",
       "\t<summary style=display:list-item;cursor:pointer>\n",
       "\t\t<strong>Levels</strong>:\n",
       "\t</summary>\n",
       "\t<style>\n",
       "\t.list-inline {list-style: none; margin:0; padding: 0}\n",
       "\t.list-inline>li {display: inline-block}\n",
       "\t.list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "\t</style>\n",
       "\t<ol class=list-inline><li>'setosa'</li><li>'versicolor'</li><li>'virginica'</li></ol>\n",
       "</details></dd>\n",
       "\t<dt>$posterior</dt>\n",
       "\t\t<dd><table class=\"dataframe\">\n",
       "<caption>A matrix: 1 × 3 of type dbl</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>setosa</th><th scope=col>versicolor</th><th scope=col>virginica</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>1</th><td>6.24321e-146</td><td>4.105739e-87</td><td>1</td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "</dd>\n",
       "\t<dt>$x</dt>\n",
       "\t\t<dd><table class=\"dataframe\">\n",
       "<caption>A matrix: 1 × 2 of type dbl</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>LD1</th><th scope=col>LD2</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>1</th><td>28.5506</td><td>99.33428</td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "</dd>\n",
       "</dl>\n"
      ],
      "text/latex": [
       "\\begin{description}\n",
       "\\item[\\$class] virginica\n",
       "\\emph{Levels}: \\begin{enumerate*}\n",
       "\\item 'setosa'\n",
       "\\item 'versicolor'\n",
       "\\item 'virginica'\n",
       "\\end{enumerate*}\n",
       "\n",
       "\\item[\\$posterior] A matrix: 1 × 3 of type dbl\n",
       "\\begin{tabular}{r|lll}\n",
       "  & setosa & versicolor & virginica\\\\\n",
       "\\hline\n",
       "\t1 & 6.24321e-146 & 4.105739e-87 & 1\\\\\n",
       "\\end{tabular}\n",
       "\n",
       "\\item[\\$x] A matrix: 1 × 2 of type dbl\n",
       "\\begin{tabular}{r|ll}\n",
       "  & LD1 & LD2\\\\\n",
       "\\hline\n",
       "\t1 & 28.5506 & 99.33428\\\\\n",
       "\\end{tabular}\n",
       "\n",
       "\\end{description}\n"
      ],
      "text/markdown": [
       "$class\n",
       ":   virginica\n",
       "**Levels**: 1. 'setosa'\n",
       "2. 'versicolor'\n",
       "3. 'virginica'\n",
       "\n",
       "\n",
       "\n",
       "$posterior\n",
       ":   \n",
       "A matrix: 1 × 3 of type dbl\n",
       "\n",
       "| <!--/--> | setosa | versicolor | virginica |\n",
       "|---|---|---|---|\n",
       "| 1 | 6.24321e-146 | 4.105739e-87 | 1 |\n",
       "\n",
       "\n",
       "$x\n",
       ":   \n",
       "A matrix: 1 × 2 of type dbl\n",
       "\n",
       "| <!--/--> | LD1 | LD2 |\n",
       "|---|---|---|\n",
       "| 1 | 28.5506 | 99.33428 |\n",
       "\n",
       "\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "$class\n",
       "[1] virginica\n",
       "Levels: setosa versicolor virginica\n",
       "\n",
       "$posterior\n",
       "        setosa   versicolor virginica\n",
       "1 6.24321e-146 4.105739e-87         1\n",
       "\n",
       "$x\n",
       "      LD1      LD2\n",
       "1 28.5506 99.33428\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "predict(l1, newdata=x0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "handled-newton",
   "metadata": {},
   "source": [
    "判为 `virginica` 类型，即品种 3。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "legal-survey",
   "metadata": {},
   "source": [
    "带入 qda 模型："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "available-connecticut",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<dl>\n",
       "\t<dt>$class</dt>\n",
       "\t\t<dd>virginica\n",
       "<details>\n",
       "\t<summary style=display:list-item;cursor:pointer>\n",
       "\t\t<strong>Levels</strong>:\n",
       "\t</summary>\n",
       "\t<style>\n",
       "\t.list-inline {list-style: none; margin:0; padding: 0}\n",
       "\t.list-inline>li {display: inline-block}\n",
       "\t.list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "\t</style>\n",
       "\t<ol class=list-inline><li>'setosa'</li><li>'versicolor'</li><li>'virginica'</li></ol>\n",
       "</details></dd>\n",
       "\t<dt>$posterior</dt>\n",
       "\t\t<dd><table class=\"dataframe\">\n",
       "<caption>A matrix: 1 × 3 of type dbl</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>setosa</th><th scope=col>versicolor</th><th scope=col>virginica</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>1</th><td>0</td><td>9.004664e-244</td><td>1</td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "</dd>\n",
       "</dl>\n"
      ],
      "text/latex": [
       "\\begin{description}\n",
       "\\item[\\$class] virginica\n",
       "\\emph{Levels}: \\begin{enumerate*}\n",
       "\\item 'setosa'\n",
       "\\item 'versicolor'\n",
       "\\item 'virginica'\n",
       "\\end{enumerate*}\n",
       "\n",
       "\\item[\\$posterior] A matrix: 1 × 3 of type dbl\n",
       "\\begin{tabular}{r|lll}\n",
       "  & setosa & versicolor & virginica\\\\\n",
       "\\hline\n",
       "\t1 & 0 & 9.004664e-244 & 1\\\\\n",
       "\\end{tabular}\n",
       "\n",
       "\\end{description}\n"
      ],
      "text/markdown": [
       "$class\n",
       ":   virginica\n",
       "**Levels**: 1. 'setosa'\n",
       "2. 'versicolor'\n",
       "3. 'virginica'\n",
       "\n",
       "\n",
       "\n",
       "$posterior\n",
       ":   \n",
       "A matrix: 1 × 3 of type dbl\n",
       "\n",
       "| <!--/--> | setosa | versicolor | virginica |\n",
       "|---|---|---|---|\n",
       "| 1 | 0 | 9.004664e-244 | 1 |\n",
       "\n",
       "\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "$class\n",
       "[1] virginica\n",
       "Levels: setosa versicolor virginica\n",
       "\n",
       "$posterior\n",
       "  setosa    versicolor virginica\n",
       "1      0 9.004664e-244         1\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "predict(qd, newdata=x0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "stupid-pennsylvania",
   "metadata": {},
   "source": [
    "两种模型给出了相同的预测结果。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "jewish-deposit",
   "metadata": {},
   "source": [
    "## (4)\n",
    "\n",
    "使用全部指标。\n",
    "\n",
    "### lda"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "diagnostic-beads",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Call:\n",
       "lda(Species ~ ., data = iris)\n",
       "\n",
       "Prior probabilities of groups:\n",
       "    setosa versicolor  virginica \n",
       " 0.3333333  0.3333333  0.3333333 \n",
       "\n",
       "Group means:\n",
       "           Sepal.Length Sepal.Width Petal.Length Petal.Width\n",
       "setosa            5.006       3.428        1.462       0.246\n",
       "versicolor        5.936       2.770        4.260       1.326\n",
       "virginica         6.588       2.974        5.552       2.026\n",
       "\n",
       "Coefficients of linear discriminants:\n",
       "                    LD1         LD2\n",
       "Sepal.Length  0.8293776  0.02410215\n",
       "Sepal.Width   1.5344731  2.16452123\n",
       "Petal.Length -2.2012117 -0.93192121\n",
       "Petal.Width  -2.8104603  2.83918785\n",
       "\n",
       "Proportion of trace:\n",
       "   LD1    LD2 \n",
       "0.9912 0.0088 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "l2 = lda(Species ~ ., data=iris); l2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "complicated-introduction",
   "metadata": {},
   "source": [
    "误判："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "spoken-tobago",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "$tab\n",
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         48         2\n",
       "  virginica       0          1        49\n",
       "\n",
       "$err\n",
       "         class posterior.setosa posterior.versicolor posterior.virginica\n",
       "71   virginica     7.408118e-28            0.2532282           0.7467718\n",
       "84   virginica     4.241952e-32            0.1433919           0.8566081\n",
       "134 versicolor     1.283891e-28            0.7293881           0.2706119\n",
       "        x.LD1      x.LD2 true.group\n",
       "71  -3.715896  1.0445144 versicolor\n",
       "84  -4.498466 -0.8827499 versicolor\n",
       "134 -3.815160 -0.9429859  virginica\n",
       "\n",
       "$err.ratio\n",
       "[1] 0.02\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pred2 <- predict(l2)\n",
    "prederr(pred2, Species)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "shared-exercise",
   "metadata": {},
   "source": [
    "交叉确认误判率："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "assumed-conflict",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "$tab\n",
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         48         2\n",
       "  virginica       0          1        49\n",
       "\n",
       "$err\n",
       "         class       setosa versicolor virginica true.group\n",
       "71   virginica 1.302246e-28 0.17727267 0.8227273 versicolor\n",
       "84   virginica 1.125494e-33 0.09924153 0.9007585 versicolor\n",
       "134 versicolor 5.464475e-29 0.78762376 0.2123762  virginica\n",
       "\n",
       "$err.ratio\n",
       "[1] 0.02\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "l2.cv <- lda(Species ~ ., data=iris, CV=TRUE)\n",
    "prederr(data.frame(class=l2.cv$class, l2.cv$posterior), Species)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "upset-leave",
   "metadata": {},
   "source": [
    "误判率比只用两个指标明显提升了。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "impaired-balloon",
   "metadata": {},
   "source": [
    "### qda"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "chief-cache",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Call:\n",
       "qda(Species ~ ., data = iris)\n",
       "\n",
       "Prior probabilities of groups:\n",
       "    setosa versicolor  virginica \n",
       " 0.3333333  0.3333333  0.3333333 \n",
       "\n",
       "Group means:\n",
       "           Sepal.Length Sepal.Width Petal.Length Petal.Width\n",
       "setosa            5.006       3.428        1.462       0.246\n",
       "versicolor        5.936       2.770        4.260       1.326\n",
       "virginica         6.588       2.974        5.552       2.026"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "qd2 = qda(Species ~ ., data=iris); qd2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "hazardous-oriental",
   "metadata": {},
   "source": [
    "误判："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "likely-timothy",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "$tab\n",
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         48         2\n",
       "  virginica       0          1        49\n",
       "\n",
       "$err\n",
       "         class posterior.setosa posterior.versicolor posterior.virginica\n",
       "71   virginica    1.052723e-103            0.3359442           0.6640558\n",
       "84   virginica    4.102009e-114            0.1543483           0.8456517\n",
       "134 versicolor    4.550670e-111            0.6049611           0.3950389\n",
       "    true.group\n",
       "71  versicolor\n",
       "84  versicolor\n",
       "134  virginica\n",
       "\n",
       "$err.ratio\n",
       "[1] 0.02\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "predq2 <- predict(qd2)\n",
    "prederr(predq2, Species)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "stainless-bones",
   "metadata": {},
   "source": [
    "交叉确认误判率："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "purple-advocacy",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "$tab\n",
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         47         3\n",
       "  virginica       0          1        49\n",
       "\n",
       "$err\n",
       "         class        setosa versicolor virginica true.group\n",
       "69   virginica  1.376175e-89 0.31342177 0.6865782 versicolor\n",
       "71   virginica 1.329043e-103 0.16164225 0.8383577 versicolor\n",
       "84   virginica 4.504693e-114 0.07133282 0.9286672 versicolor\n",
       "134 versicolor 4.988739e-111 0.66319758 0.3368024  virginica\n",
       "\n",
       "$err.ratio\n",
       "[1] 0.02666667\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "qd2.cv <- qda(Species ~ ., data=iris, CV=TRUE)\n",
    "prederr(data.frame(class=qd2.cv$class, qd2.cv$posterior), Species)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "communist-inquiry",
   "metadata": {},
   "source": [
    "同样，比只使用两个特征做出来的效果更好，误判率更低。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "4.0.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
